package one;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MathOptionProxy {
	private MathOption target;
	
	public MathOption getLoggingProxy(){
		MathOption proxy = null;
		ClassLoader loader = target.getClass().getClassLoader();
		Class[] interfaces = new Class[]{MathOption.class};
		
		InvocationHandler h = new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				
				System.out.println("The method "+method.getName()+" begins with " + Arrays.asList(args) );
				System.out.println(target);
                System.out.println(args);
                Object	result=	method.invoke(target, args);
				System.out.println("The method " + method.getName() + " end " + method.invoke(target, args));
				return result;
			}
		};
		proxy = (MathOption) Proxy.newProxyInstance(loader, interfaces, h);
		return proxy;
	}
	
	public MathOptionProxy(MathOption target){
		this.target = target;
	}
	public void beforeMethod(){
		System.out.println("The method begins");
	}
}
